<?php
namespace app\admin\controller;
use app\service\roominfoService;
use think\facade\Db;
use think\facade\View;

class Contract extends Common{
	public function contractList(){
		$com = $this->pidTokenId();
		$whe['community_id'] = array('in',$com['community_id']);
		$list=Db::table("v2_contract")->where($whe)->select()->toArray();
		View::assign("list",$list);
		return View::fetch("v2_contract/contract_list");
	}

	public function contractAdd(){
		if(request()->isPost()){
		    $data = request()->post();
		    $data["start_time"]=strtotime($data["start_time"]);
		    $data["end_time"]=strtotime($data["end_time"]);
		    $data["create_time"]=time();
		    $supplierinfo=Db::table('v2_supplier')->where('id',$data["supplier_id"])->find();

		    $community=Db::name('community')->where('community_id',$supplierinfo["community_id"])->find();
           	$property=Db::table("property")->where('id',$community["property_id"])->find();

		    $data["community_name"]=$supplierinfo["community_name"];
		    $data["community_id"]=$supplierinfo["community_id"];
		    $data["supplier_name"]=$supplierinfo["supplier_name"];
		    $data["property_id"]=$community["property_id"];
           	$data["property_name"]=$property["name"];
		    $res=Db::table("v2_contract")->insert($data);
           	if($res){
                $this->success('合同添加成功',url('supplierAdd'));
            }else{
                $this->error('合同添加成功',url('supplierAdd'));
            }
		}else{
			$com = $this->pidTokenId();
			$whe['community_id'] = array('in',$com['community_id']);
			$communitylist=(new roominfoService())->communityList($whe);;
			View::assign('communitylist',$communitylist);
			$supplier=Db::table("v2_supplier")->where($whe)->order("id desc")->select()->toArray();
			View::assign("supplier",$supplier);
			return View::fetch("v2_contract/contract_add");
		}
	}

	public function supplierList(){
		$com = $this->pidTokenId();
		$whe['community_id'] = array('in',$com['community_id']);
        $communitylist = (new roominfoService())->communityList($whe);
        $community_id = $com['community_id'];
        View::assign('communitylist',$communitylist);

        $list=Db::table("v2_supplier")->select()->toArray();
        View::assign('list',$list);
        
        return View::fetch("v2_contract/supplier_list");
	}

	public function supplierAdd(){
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
           	$community=Db::name('community')->where('community_id',$data["community_id"])->find();
           	$property=Db::table("property")->where('id',$community["property_id"])->find();
           	$data["community_name"]=$community["community_name"];
           	$data["property_id"]=$community["property_id"];
           	$data["property_name"]=$property["name"];
           	$res=Db::table("v2_supplier")->insert($data);
           	if($res){
                $this->success('添加服务商成功',url('supplierAdd'));
            }else{
                $this->error('添加服务商成功',url('supplierAdd'));
            }
        }else{
        	//查询小区信息
	        $com = $this->pidTokenId();
	        $where['community_id'] = array('in',$com['community_id']);
	        $communitylist = (new roominfoService())->communityList($where);
	        View::assign('communitylist',$communitylist);
            return View::fetch('v2_contract/supplier_add');
        }
	}
}